﻿//字符串连接
function StringBuilder() {
    this.strings = new Array();
}
StringBuilder.prototype.append = function (str) {
    this.strings.push(str);
}
StringBuilder.prototype.toString = function () {
    if (arguments.length == 0)
        return this.strings.join("");
    else
        return this.strings.join(arguments[0]);
}
StringBuilder.prototype.clear = function () {
    this.strings.clear();
}
//日期格式化
Date.prototype.format = function (format) {
    var o = {
        "M+": this.getMonth() + 1, //month
        "d+": this.getDate(),    //day
        "h+": this.getHours(),   //hour
        "m+": this.getMinutes(), //minute
        "s+": this.getSeconds(), //second
        "q+": Math.floor((this.getMonth() + 3) / 3),  //quarter
        "S": this.getMilliseconds() //millisecond
    }
    if (/(y+)/.test(format)) format = format.replace(RegExp.$1,
        (this.getFullYear() + "").substr(4 - RegExp.$1.length));
    for (var k in o) if (new RegExp("(" + k + ")").test(format))
        format = format.replace(RegExp.$1,
        RegExp.$1.length == 1 ? o[k] :
        ("00" + o[k]).substr(("" + o[k]).length));
    return format;
}
Date.prototype.AddDays = function (days) {
    this.setDate(this.getDate() + days);
    return this;
}
Date.prototype.AddMonths = function (months) {
    this.setMonth(this.getMonth() + months);
    return this;
}
Date.prototype.AddYears = function (years) {
    this.setYear(this.getFullYear() + years);
    return this;
}
Date.prototype.AddHours = function (hours) {
    this.setHours(this.getHours() + hours);
    return this;
}
Date.prototype.AddMinutes = function (minutes) {
    this.setMinutes(this.getMinutes() + minutes);
    return this;
}
Date.prototype.AddSeconds = function (seconds) {
    this.setSeconds(this.getSeconds() + seconds);
    return this;
}
//调试日志工具(在浏览器控制台输出调试信息)
var loggerObject = new Object();
loggerObject.IsWriteLog = true;
//输出简单文本
loggerObject.WriteLogText = function (strLogContent) {
    try {
        if (this.IsWriteLog) {
            console.log(strLogContent);
        }
    } catch (e) {
    }
};
//输出对象
loggerObject.WritelogObject = function (obj, strObjName) {
    try {
        if (this.IsWriteLog) {
            console.log(obj, strObjName);
        }
    } catch (e) {
    }
}

function IsDate(str) {
    var date = str;
    var result = date.match(/^(\d{1,4})(-|\/)(\d{1,2})\2(\d{1,2})$/);

    if (result == null)
        return false;
    var d = new Date(result[1], result[3] - 1, result[4]);
    return (d.getFullYear() == result[1] && (d.getMonth() + 1) == result[3] && d.getDate() == result[4]);

}

function IsEMail(str) {
    var reg = /^([0-9A-Za-z\-_\.]+)@([0-9a-z]+(\.[0-9a-z]+)?(\.[0-9a-z]+)?\.[a-z]{2,3}(\.[a-z]{2})?)$/g;
    return reg.test(str);
}
function IsPhone(str) {
    var reg = /((\(\d{2,3}\))|(\d{3}\-))?1\d{10}$/;
    return reg.test(str);
}
function IsMobilePhone(str) {
    var reg = /^1\d{10}$/;
    return reg.test(str);
}
//最多保留两位小数
function IsTwoDecimal(str) {
    var reg = /^[1-9]+\d*$|^0\.\d{1,2}$|^[1-9]+[0-9]*\.\d{1,2}$/;
    return reg.test(str);
}
//校验非负数
function IsInteger(str) {
    var reg = /^(0|[1-9][0-9]*)$/;
    return reg.test(str);
}
//校验纯数字
function IsNumber(str) {
    var reg = /^([0-9]*)$/;
    return reg.test(str);
}
//匹配中文和英文字符
function IsCharacter(str) {
    var reg = /^[\u4e00-\u9fa5a-zA-Z]+$/;
    return reg.test(str);
}
//匹配中文汉字
function IsChinese(str) {
    var reg = /^[\u4e00-\u9fa5]+$/;
    return reg.test(str);
}
//检测VIN码是否符合规则，返回值 bool 型
function ChkVIN(thisVIN) {
    var currentVIN = thisVIN.toUpperCase();
    if (currentVIN.length != 17) {
        return false;
    }

    var wrong = /I|O|Q/ig;
    if (wrong.test(currentVIN)) {
        return false;
    }

    var weighting = new Array(8, 7, 6, 5, 4, 3, 2, 10, 0, 9, 8, 7, 6, 5, 4, 3, 2);

    var sum = 0;
    for (var i = 0; i < currentVIN.length; i++) {
        switch (currentVIN.charAt(i)) {
            case "0":
            case "1":
            case "2":
            case "3":
            case "4":
            case "5":
            case "6":
            case "7":
            case "8":
            case "9":
                sum += currentVIN.charAt(i) * weighting[i];
                break;
            case "A":
            case "B":
            case "C":
            case "D":
            case "E":
            case "F":
            case "G":
            case "H":
                sum += (currentVIN.charCodeAt(i) - 64) * weighting[i];
                break;
            case "J":
            case "K":
            case "L":
            case "M":
            case "N":

            case "P":

            case "R":
                sum += (currentVIN.charCodeAt(i) - 73) * weighting[i];
                break;
            case "S":
            case "T":
            case "U":
            case "V":
            case "W":
            case "X":
            case "Y":
            case "Z":
                sum += (currentVIN.charCodeAt(i) - 81) * weighting[i];
                break;
        }
    }

    var chkBit = sum % 11;
    if (chkBit < 10 && currentVIN.charAt(8) == chkBit.toString()) {
        return true;
    }
    else if (currentVIN.charAt(8) == "X") {
        return true;
    }
    return false;
}

var Wi = [7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2, 1];    // 加权因子   
var ValideCode = [1, 0, 10, 9, 8, 7, 6, 5, 4, 3, 2];            // 身份证验证位值.10代表X   
function IdCardValidate(idCard) {
    idCard = trim(idCard.replace(/ /g, ""));               //去掉字符串头尾空格                     
    if (idCard.length == 15) {
        return isValidityBrithBy15IdCard(idCard);       //进行15位身份证的验证    
    } else if (idCard.length == 18) {
        var a_idCard = idCard.split("");                // 得到身份证数组   
        if (isValidityBrithBy18IdCard(idCard) && isTrueValidateCodeBy18IdCard(a_idCard)) {   //进行18位身份证的基本验证和第18位的验证
            return true;
        } else {
            return false;
        }
    } else {
        return false;
    }
}
/**  
* 判断身份证号码为18位时最后的验证位是否正确  
* @param a_idCard 身份证号码数组  
* @return  
*/
function isTrueValidateCodeBy18IdCard(a_idCard) {
    var sum = 0;                             // 声明加权求和变量   
    if (a_idCard[17].toLowerCase() == 'x') {
        a_idCard[17] = 10;                    // 将最后位为x的验证码替换为10方便后续操作   
    }
    for (var i = 0; i < 17; i++) {
        sum += Wi[i] * a_idCard[i];            // 加权求和   
    }
    valCodePosition = sum % 11;                // 得到验证码所位置   
    if (a_idCard[17] == ValideCode[valCodePosition]) {
        return true;
    } else {
        return false;
    }
}
/**  
* 验证18位数身份证号码中的生日是否是有效生日  
* @param idCard 18位书身份证字符串  
* @return  
*/
function isValidityBrithBy18IdCard(idCard18) {
    var year = idCard18.substring(6, 10);
    var month = idCard18.substring(10, 12);
    var day = idCard18.substring(12, 14);
    var temp_date = new Date(year, parseFloat(month) - 1, parseFloat(day));
    // 这里用getFullYear()获取年份，避免千年虫问题   
    if (temp_date.getFullYear() != parseFloat(year)
          || temp_date.getMonth() != parseFloat(month) - 1
          || temp_date.getDate() != parseFloat(day)) {
        return false;
    } else {
        return true;
    }
}
/**  
* 验证15位数身份证号码中的生日是否是有效生日  
* @param idCard15 15位书身份证字符串  
* @return  
*/
function isValidityBrithBy15IdCard(idCard15) {
    var year = idCard15.substring(6, 8);
    var month = idCard15.substring(8, 10);
    var day = idCard15.substring(10, 12);
    var temp_date = new Date(year, parseFloat(month) - 1, parseFloat(day));
    // 对于老身份证中的你年龄则不需考虑千年虫问题而使用getYear()方法   
    if (temp_date.getYear() != parseFloat(year)
              || temp_date.getMonth() != parseFloat(month) - 1
              || temp_date.getDate() != parseFloat(day)) {
        return false;
    } else {
        return true;
    }
}
/**  
* 通过身份证判断是男是女  
* @param idCard 15/18位身份证号码   
* @return 'female'-女、'male'-男  
*/
function maleOrFemalByIdCard(idCard) {
    idCard = trim(idCard.replace(/ /g, ""));        // 对身份证号码做处理。包括字符间有空格。   
    if (idCard.length == 15) {
        if (idCard.substring(14, 15) % 2 == 0) {
            return 'female';
        } else {
            return 'male';
        }
    } else if (idCard.length == 18) {
        if (idCard.substring(14, 17) % 2 == 0) {
            return 'female';
        } else {
            return 'male';
        }
    } else {
        return null;
    }
}
//去掉字符串头尾空格   
function trim(str) {
    return str.replace(/(^\s*)|(\s*$)/g, "");
}

function delCookie() {
    var domainName = "meilianyougou.com";
    $.cookie('MerAcount', null, { expires: 7, path: '/', domain: domainName });
    $.cookie('MerID', null, { expires: 7, path: '/', domain: domainName });
    $.cookie('MerName', null, { expires: 7, path: '/', domain: domainName });
    $.cookie('MerType', null, { expires: 7, path: '/', domain: domainName });
    location.reload();
}

//有效车牌 - 特殊车辆不保
function validLicenceNo(value) {
    var ln = /^([\u4e00-\u9fa5]){1}([a-zA-Z]){1}([a-z0-9A-Z]){5}$/;
    var result = ln.test(value);
    if (value.toUpperCase() == 'WJ' || value.indexOf('警') != -1) {
        result = false;
    }
    return result;
}

//是否客户端浏览
function MobileBrower() {
    var regex_match = /(nokia|iphone|android|motorola|^mot-|softbank|foma|docomo|kddi|up.browser|up.link|htc|dopod|blazer|netfront|helio|hosin|huawei|novarra|CoolPad|webos|techfaith|palmsource|blackberry|alcatel|amoi|ktouch|nexian|samsung|^sam-|s[cg]h|^lge|ericsson|philips|sagem|wellcom|bunjalloo|maui|symbian|smartphone|midp|wap|phone|windows ce|iemobile|^spice|^bird|^zte-|longcos|pantech|gionee|^sie-|portalmmm|jigs browser|hiptop|^benq|haier|^lct|operas*mobi|opera*mini|320x320|240x320|176x220)/i;
    var u = navigator.userAgent;
    if (null == u) {
        return true;
    }
    var result = regex_match.exec(u);
    if (null == result) {
        return false
    } else {
        return true
    }
}

function QueryString(item) {
    var sValue = location.search.match(new RegExp("[\?\&]" + item + "=([^\&]*)(\&?)", "i"))
    return sValue ? sValue[1] : sValue
}